﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 27.11.2014
 * Time: 15:59
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;

using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Region.Forms
{

  /// <summary>
  /// Description of PoruchForm.
  /// </summary>
  public partial class PoruchForm : Form, IDBForm
  {
    private Engine m_engine;
    private Poruch m_poruch;
    private Kont m_kont;
    ObjectFormUtil m_reader;
    //IDBForm интерфейс
    public ObjectFormUtil i_reader{get{return m_reader;}}
    public DBObject i_object{get{return m_poruch;}}
    public void readPoruch()
    {
      this.Text = "Редактирование поручения №" + m_poruch.PoruchNum.ToString() + " от " + m_poruch.PoruchDate.ToShortDateString();
      m_reader.get();
      //Вывод информации о контрагенте
      EditKont.Enabled = KontObjectList.Enabled = m_poruch.poleAccess("KontID");
      m_kont = null;
      KontName.Text = "[Не указан контрагент]";
      INN.Text = "";
      OGRN.Text = "";
      KontGosReg.Text = "";
      KontForm.Text = "";
      KontType.Text = "";
      ULAddress.Text = "";
      FKAddress.Text = "";
      AddKontObject.Enabled = false;
      KontObjectList.Items.Clear();
      
      if (m_poruch.KontID != 0)
      {
          m_kont = m_poruch.Kont;
          KontName.Text = m_kont.KontName;
          INN.Text = m_kont.INN;
          OGRN.Text = m_kont.OGRN;
          KontGosReg.Text = m_kont.KontGosReg.ToShortDateString();
          KontForm.Text = m_engine.KontForm[m_kont.KontForm];
          KontType.Text = m_engine.KontType[m_kont.KontTypeID];
          ULAddress.Text = m_kont.KontULAddress;
          FKAddress.Text = m_kont.KontFKAddress;
          AddKontObject.Enabled = m_poruch.poleAccess("KontID");
          KontObjectList.Items.Clear();
          Dictionary<Int32, String> Raions = m_engine.Raions;
          Dictionary<Int32, Poruch.PoruchObject> RO = m_poruch.PoruchObjects;
          foreach (KeyValuePair<Int32, Kont.KontObject> K in m_kont.KontObjects)
          {
              //User U = m_engine.getUser();
              Int32 KontObjectID = K.Key;
              String S = K.Value.KontObjectName + ": " + Engine.getAddressString(K.Value.KontObjectPostIndex, K.Value.KontObjectRegion, Raions[K.Value.KontObjectRaionID], K.Value.KontObjectGorod, K.Value.KontObjectNaspunkt, K.Value.KontObjectStreet, K.Value.KontObjectDom, K.Value.KontObjectKorpus, K.Value.KontObjectKvart);
              KontObjectList.Items.Add(new Utils.ListItemValue(S, KontObjectID), RO.ContainsKey(KontObjectID));
          }
      }
      PoruchFun.Nodes.Clear();
      var PD = new Dictionary<String,TreeNode>();
      var PFPL = new PoruchFunPartList(m_engine);
      PFPL.Filter();
      var FunPartFont = new Font(this.Font,FontStyle.Bold);
      for(Int32 i=0;i<PFPL.NumLines;i++)
      {
        var l = PFPL.getLine(i);
        var node = new TreeNode();
        node.Checked = false;
        node.NodeFont = FunPartFont;
        node.Text = l.PoruchFunPartName;
        node.ToolTipText = l.PoruchFunPartComment;
        PD[l.PoruchFunPart] = node;
        if (PD.ContainsKey(l.ParentPoruchFunPart))
        {
          PD[l.ParentPoruchFunPart].Nodes.Add(node);
        }
        else
        {
          PoruchFun.Nodes.Add(node);
        }
      }
      var PDFun = new Dictionary<String,TreeNode>(); 
      var PFL = new PoruchFunList(m_engine);
      PFL.Filter();      
      for(Int32 i=0;i<PFL.NumLines;i++)
      {
        var l = PFL.getLine(i);
        var node = new TreeNode();
        node.Checked = false;
        node.Tag = l.PoruchFunID;
        //node.NodeFont = FunPartFont;
        node.Text = l.PoruchFun;
        node.ToolTipText = l.PoruchFunComment;
        PDFun[l.PoruchFunID] = node;
        if (PD.ContainsKey(l.PoruchFunPart))
        {
          PD[l.PoruchFunPart].Nodes.Add(node);
        }
      }            
      //PoruchFunPartList
    }
    public PoruchForm(Engine engine, Int32 PoruchID)
    {
      m_engine = engine;
      m_poruch = m_engine.getPoruch(PoruchID);
      m_reader = new ObjectFormUtil(m_engine, m_poruch);
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      IspUserID.engine = engine;
      OtdelID.engine = engine;
      Dictionary<String,Object> ListExProcessor = new Dictionary<String,Object>()
      {
        
      };      
      DBForm.addEditors(this,this,ListExProcessor);
      readPoruch();
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
    }
    private void savePoruch()
    {
        Int32 Pred;
        m_reader.set();        
        Dictionary<Int32, Poruch.PoruchObject> RO1 = m_poruch.PoruchObjects;
        Dictionary<Int32, bool> RO2 = new Dictionary<Int32, bool>();
        foreach (KeyValuePair<Int32, Poruch.PoruchObject> I in m_poruch.PoruchObjects) 
          RO2[(Int32)I.Key] = false;//Пометить всех исполнителей на удаление
        foreach (Utils.ListItemValue LIV in KontObjectList.CheckedItems) RO2[(Int32)LIV.Value] = true;//Пометить специалистов на добавление
        foreach (KeyValuePair<Int32, bool> I in RO2) if (I.Value) m_poruch.AddPoruchObject(I.Key); else m_poruch.DelPoruchObject(I.Key);
        m_poruch.save();
    }    
    void EditKontClick(object sender, EventArgs e)
    {
      if (m_poruch.poleAccess("KontID"))
      {
          var KL = new Forms.RegionKontList(m_engine);
          if (KL.ShowDialog() == DialogResult.OK)
          {
              m_poruch.KontID = KL.SelectKontID;
              savePoruch();
              readPoruch();
          }
      }       
    }
    
    void ToolSaveClick(object sender, EventArgs e)
    {
      savePoruch();
    }
    Boolean CancelCancel = false;
    void PoruchFunBeforeCheck(object sender, TreeViewCancelEventArgs e)
    {
      if (CancelCancel) return;
      if (e.Node.Tag==null)
      {
        e.Cancel = true;
      }
    }
    void PoruchFunAfterCheck(object sender, TreeViewEventArgs e)
    {
      if (e.Node.Tag!=null)
      {
        CancelCancel = true;
        TreeNode n = e.Node;
        while((n=n.Parent)!=null)
        {
          Boolean ch = false;
          foreach(TreeNode sn in n.Nodes)
            if (sn.Checked)
              ch=true;
          n.Checked = ch;          
        }
        CancelCancel = false;
      }
    }
    void Panel4Paint(object sender, PaintEventArgs e)
    {
  
    }
  }
}
